package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DTO.*;
import com.groupfour.attendancemanagementsystem.entity.VO.CourseVO;
import com.groupfour.attendancemanagementsystem.service.CourseService;
import com.groupfour.attendancemanagementsystem.utils.JwtHelper;
import com.groupfour.attendancemanagementsystem.utils.Result;
import com.groupfour.attendancemanagementsystem.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author xin
 * @date 2022/4/19 15:50
 */
@RestController
@RequestMapping("/courseManagement")
public class CourseManagementController {

//    private final Integer pageSize = 10;

    @Autowired
    private CourseService courseService;

    /**
     * 管理员端
     */

    /**
     * @author 邹代鑫
     * @param
     * @describe 管理员查询课程名称
     */
    @GetMapping("/findCourseName")
    public Result findCourseName(){
        return Result.ok(this.courseService.findCourseName());
    }

    /**
     * @author 邹代鑫
     * @param
     * @describe 管理员查询实训老师名称
     */
    @GetMapping("/findTrainTeacher")
    public Result findTrainTeacher(){
        return Result.ok(this.courseService.findTrainTeacher());
    }

    /**
     * @author 邹代鑫
     * @param
     * @describe 管理员查询责任老师名称
     */
    @GetMapping("/findDutyTeacher")
    public Result findDutyTeacher(){
        return Result.ok(this.courseService.findDutyTeacher());
    }


    /**
     *
     * @param courseDTO
     * @param pageNum
     * @return
     * @describe 条件查询
     */
    @PostMapping("/findCourseByCourseDTO/{pageNum}/{pageSize}")
    public Result findCourseByCourseDTO(
            @RequestBody CourseDTO courseDTO,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        Map<String, Object> courseByCourseDTO = this.courseService.findCourseByCourseDTO(courseDTO, pageNum, pageSize);
        Long num = (Long) courseByCourseDTO.get("pageCount");
        if (num > 0) {
            return Result.ok(courseByCourseDTO);
        }
        return Result.fail(ResultCodeEnum.SELECT_ERROR.getMessage());
    }

    /**
     * @author 邹代鑫
     * @param pageNum
     * @return
     * @describe 管理员查询所有课程
     */
    @GetMapping("/findAllCourse/{pageNum}/{pageSize}")
    public Result findAllCourse(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize){
        Map<String,Object> allCourse = this.courseService.findAllCourse(pageNum,pageSize);
        return Result.ok(allCourse);
    }

    /**
     * @author 邹代鑫
     * @param courseDTOByAdd
     * @return
     * @describe 管理员添加课程
     */
    @PostMapping("/addCourse")
    public  Result addCourse(@RequestBody CourseDTOByAdd courseDTOByAdd){
        if (this.courseService.addCourse(courseDTOByAdd) == 0){
            return Result.fail("已存在课程名");
        }else{
            return Result.ok(1);
        }
    }

    @PostMapping("/updateCourse/{courseCode}")
    public Result updateCourse(
            @RequestBody CourseDTOByUpdate courseDTOByUpdate,@PathVariable("courseCode") String courseCode){
        return Result.ok(this.courseService.updateCourse(courseDTOByUpdate,courseCode));
    }

    @GetMapping("/chooseGroup")
    public Result chooseGroup(){
        return Result.ok(this.courseService.findGroupNames());
    }

    @GetMapping("/chooseStudent")
    public Result chooseStudent(String groupCode){
        return Result.ok(this.courseService.chooseStudent(groupCode));
    }

    @PostMapping("/addStudentInCourse")
    public Result addStudentInCourse(
            @RequestBody StudentInCourseDTO studentInCourseDTO
            ){
        this.courseService.addStudentInCourse(studentInCourseDTO);
        return Result.ok(1);
    }

    @GetMapping("/deleteCourse/{courseCode}")
    public Result deleteCourse(@PathVariable("courseCode") String courseCode){
        return Result.ok(this.courseService.deleteCourse(courseCode));
    }

    /**
     * 学生端
     */
    @GetMapping("/findAllStudentCourse/{pageNum}/{pageSize}")
    public Result findAllStudentCourse(
            @RequestHeader("token") String token,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        String studentCode = JwtHelper.getUserCode(token);
        return Result.ok(this.courseService.findAllStudentCourse(studentCode,pageNum,pageSize));
    }

    @PostMapping("/findStudentCourse/{pageNum}/{pageSize}")
    public Result findStudentCourse(
            @RequestBody CourseDTO courseDTO,
            @RequestHeader("token") String token,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        String studentCode = JwtHelper.getUserCode(token);
        return Result.ok(this.courseService.findStudentCourse(studentCode,pageNum,pageSize,courseDTO));
    }
    /**
     * 教师端
     */
    @GetMapping("/findAllTeacherCourse/{pageNum}/{pageSize}")
    public Result findAllTeacherCourse(
            @RequestHeader("token") String token,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        String teacherCode = JwtHelper.getUserCode(token);
        return Result.ok(this.courseService.findAllTeacherCourse(teacherCode,pageNum,pageSize));
    }

    @PostMapping("/findTeacherCourse/{pageNum}/{pageSize}")
    public Result findTeacherCourse(
            @RequestBody CourseDTO courseDTO,
            @RequestHeader("token") String token,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        String teacherCode = JwtHelper.getUserCode(token);
        return Result.ok(this.courseService.findTeacherCourse(teacherCode,pageNum,pageSize,courseDTO));
    }

    @GetMapping("/findAllGroupName")
    public Result findAllGroupName(){
        return Result.ok(this.courseService.findAllGroupName());
    }

    @GetMapping("/findAllMajor")
    public Result findAllMajor(){
        return Result.ok(this.courseService.findAllMajor());
    }

    @GetMapping("/findAllClasses")
    public Result findAllClasses(){
        return Result.ok(this.courseService.findAllClasses());
    }

    @PostMapping("/checkAllStudent/{courseCode}")
    public Result checkAllStudent(
            @PathVariable("courseCode") String courseCode,
            @RequestBody CheckStudentDTO checkStudentDTO
            ){
        return Result.ok(this.courseService.checkAllStudent(courseCode,checkStudentDTO));
    }

}
